/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MRP.ProductionPlan;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.MRP.ProductionPlan.MonthlyProductionPlanBeanLocal;
import session.stateless.MRP.SaleForecast.SaleForecastBeanLocal;

/**
 *
 * @author zhozho
 */
@ManagedBean(name="plannedMonthlyDemand")
@ViewScoped
public class GenerateMonthlyProductionPlanManagerBean implements Serializable{
    @EJB
    MonthlyProductionPlanBeanLocal plannedMonthlyDemandBeanLocal;
    @EJB 
    private SaleForecastBeanLocal saleForecastBeanLocal;

    
    private String startMonth;
    private String endMonth;
    private String month;
    private List<String> monthList = new ArrayList<String>();
    private List<ColumnModel> simpleColumns;
    
    private List<DataObject> dataListA;
    private List<DataObject> dataListB;
    private List<DataObject> dataListC;
    private List<DataObject> dataListD;

    public List<DataObject> getDataListA() {
        return dataListA;
    }

    public void setDataListA(List<DataObject> dataListA) {
        this.dataListA = dataListA;
    }

    public List<DataObject> getDataListB() {
        return dataListB;
    }

    public void setDataListB(List<DataObject> dataListB) {
        this.dataListB = dataListB;
    }

    public List<DataObject> getDataListC() {
        return dataListC;
    }

    public void setDataListC(List<DataObject> dataListC) {
        this.dataListC = dataListC;
    }

    public List<DataObject> getDataListD() {
        return dataListD;
    }

    public void setDataListD(List<DataObject> dataListD) {
        this.dataListD = dataListD;
    }
    
    public List<String> getMonthList() {
        return monthList;
    }

    public void setMonthList(List<String> monthList) {
        this.monthList = monthList;
    }
    
    public GenerateMonthlyProductionPlanManagerBean() {
        
    }

    public List<ColumnModel> getSimpleColumns() {
        return simpleColumns;
    }

    public void setSimpleColumns(List<ColumnModel> simpleColumns) {
        this.simpleColumns = simpleColumns;
    }

    public String getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }

    public String getStartMonth() {
        return startMonth;
    }

    public void setStartMonth(String startMonth) {
        this.startMonth = startMonth;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }
    
    
    public void initView(PhaseEvent event){
        if(startMonth == null) 
        {
            startMonth = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("startMonth");            
        }
        if(endMonth == null) 
        {
            endMonth = (String)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("endMonth");            
        }
        monthList = plannedMonthlyDemandBeanLocal.createMonthList();
        createDynamicColumns();

    }
    
    
    private void createDynamicColumns() {  
        dataListA = new ArrayList<DataObject>();
        
        dataListA.add(gatherSalesForecastInfo("A"));
        dataListA.add(gatherProductionPlan("A"));
        dataListA.add(gatherInventory("A"));
        dataListA.add(gatherWorkingDays());
        dataListA.add(gatherCapacity());
        dataListA.add(gatherUtilization());
        dataListA.add(gatherBreakDown("A"));
        
        dataListB = new ArrayList<DataObject>();
        
        dataListB.add(gatherSalesForecastInfo("B"));
        dataListB.add(gatherProductionPlan("B"));
        dataListB.add(gatherInventory("B"));
        dataListB.add(gatherWorkingDays());
        dataListB.add(gatherCapacity());
        dataListB.add(gatherUtilization());
        dataListB.add(gatherBreakDown("B"));
        
        dataListC = new ArrayList<DataObject>();
        
        dataListC.add(gatherSalesForecastInfo("C"));
        dataListC.add(gatherProductionPlan("C"));
        dataListC.add(gatherInventory("C"));
        dataListC.add(gatherWorkingDays());
        dataListC.add(gatherCapacity());
        dataListC.add(gatherUtilization());
        dataListC.add(gatherBreakDown("C"));
        
        dataListD = new ArrayList<DataObject>();
        
        dataListD.add(gatherSalesForecastInfo("D"));
        dataListD.add(gatherProductionPlan("D"));
        dataListD.add(gatherInventory("D"));
        dataListD.add(gatherWorkingDays());
        dataListD.add(gatherCapacity());
        dataListD.add(gatherUtilization());
        dataListD.add(gatherBreakDown("D"));
        
        
        simpleColumns = new ArrayList<ColumnModel>();
        simpleColumns.add(new ColumnModel("Sales and Operation Planning", "type"));
        //
        String[] monthMap = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
        monthList = plannedMonthlyDemandBeanLocal.createMonthList();
        int startIndex = monthList.indexOf(startMonth);
        int endIndex = monthList.indexOf(endMonth);
        for (int i = startIndex; i <= endIndex; ++i) {
            int attrib = Integer.parseInt(monthList.get(i).substring(0, monthList.get(i).length()-5));            
            attrib -=1;
            simpleColumns.add(new ColumnModel(monthList.get(i), monthMap[attrib]));
        }
    }  
    
    private DataObject gatherSalesForecastInfo(String productName) {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int year = Integer.parseInt(temp1.substring(temp1.length() - 4));
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            //monthIndex = map from month name to ( Jan -> 0, ..., Dec -> 11)
            int monthIndex = temp-1;
            list[monthIndex] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getSaleForecast(productName).toString();
            
            
            //list[monthIndex] = Integer.toString(saleForecastBeanLocal.getMonthlySaleForecasts(year, monthIndex, monthIndex).get(0).getSaleForecastMonth());
            if (list[monthIndex] == null)
            {
                list[monthIndex] = "-";
            }
        }
        DataObject result = new DataObject("Sales Forecast",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherProductionPlan(String productName) {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            if (productName.compareTo("A") == 0)
            {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanA() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanA().toString();
                }
            }
            else if (productName.compareTo("B") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanB() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanB().toString();
                }
            }
            else if (productName.compareTo("C") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanC() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanC().toString();
                }
            }
            else if (productName.compareTo("D") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanD() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getProductionPlanD().toString();
                }
            }
        }
        
        DataObject result = new DataObject("Production Plan",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherInventory(String productName) {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            if (productName.compareTo("A") == 0)
            {
                if(plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryA() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryA().toString();
                }
            }
            else if (productName.compareTo("B") == 0)
            {
                if(plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryB() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryB().toString();
                }
            }
            else if (productName.compareTo("C") == 0)
            {
                if(plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryC() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryC().toString();
                }
            }
            else if (productName.compareTo("D") == 0)
            {
                if(plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryD() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getInventoryD().toString();
                }
            }
        }
        DataObject result = new DataObject("Inventory",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherWorkingDays() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getNumberOfWorkingDays().toString();
            if (list[temp-1] == null)
            {
                list[temp-1] = "-";
            }
        }
        DataObject result = new DataObject("Working Days",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherCapacity() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getTotalCapacity().toString();
            if (list[temp-1] == null)
            {
                list[temp-1] = "-";
            }
        }
        DataObject result = new DataObject("Total Capacity (hrs)",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    private DataObject gatherUtilization() {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getUtilization().toString() + "%";
            if (list[temp-1] == null)
            {
                list[temp-1] = "-";
            }
        }
        DataObject result = new DataObject("Utilization",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    
    public void generateWeekly(ActionEvent event) throws IOException
    {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("month", month);
        FacesContext.getCurrentInstance().getExternalContext().redirect("generateWeeklyProductionPlan.xhtml");
    }
    
    
    private DataObject gatherBreakDown(String productName) {
        String[] list = new String[12];
        for (int i = 0; i < 12; ++i) {
            String temp1 = monthList.get(i);
            int temp = Integer.parseInt(temp1.substring(0, temp1.length() - 5));// January - December
            if (productName.compareTo("A") == 0)
            {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownB() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownA().toString();
                }
            }
            else if (productName.compareTo("B") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownB() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownB().toString();
                }
            }
            else if (productName.compareTo("C") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownC() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownC().toString();
                }
            }
            else if (productName.compareTo("D") == 0) {
                if (plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownD() == null)
                {
                    list[temp-1] = "-";
                }
                else
                {
                    list[temp-1] = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)).getBreakDownD().toString();
                }
            }
        }
        
        DataObject result = new DataObject("Breakdown Percentage",
                                            list[0],list[1],list[2],
                                            list[3],list[4],list[5],
                                            list[6],list[7],list[8],
                                            list[9],list[10],list[11]);
        return result;
    }
    
    static public class ColumnModel implements Serializable {  
  
        private String header;  
        private String property;
  
        public ColumnModel(String header, String property) {  
            this.header = header;  
            this.property = property;  
        }  
  
        public String getHeader() {  
            return header;  
        } 
        
        public String getProperty(){
            return property;
        }
    }  
    
    static public class DataObject implements Serializable{
        private String type;
        private String jan;
        private String feb;
        private String mar;
        private String apr;
        private String may;
        private String jun;
        private String jul;
        private String aug;
        private String sep;
        private String oct;
        private String nov;
        private String dec;

        public DataObject(String type, String jan, String feb, String mar, String apr, String may, String jun, String jul, String aug, String sep, String oct, String nov, String dec) {
            this.type = type;
            this.jan = jan;
            this.feb = feb;
            this.mar = mar;
            this.apr = apr;
            this.may = may;
            this.jun = jun;
            this.jul = jul;
            this.aug = aug;
            this.sep = sep;
            this.oct = oct;
            this.nov = nov;
            this.dec = dec;
        }

        public String getApr() {
            return apr;
        }

        public String getAug() {
            return aug;
        }

        public String getDec() {
            return dec;
        }

        public String getFeb() {
            return feb;
        }

        public String getJan() {
            return jan;
        }

        public String getJul() {
            return jul;
        }

        public String getJun() {
            return jun;
        }

        public String getMar() {
            return mar;
        }

        public String getMay() {
            return may;
        }

        public String getNov() {
            return nov;
        }

        public String getOct() {
            return oct;
        }

        public String getSep() {
            return sep;
        }

        public String getType() {
            return type;
        }
    }
}
